//获取当前日期（XXXX-XX-XX）
export function getCurDate() {
	let now = new Date();
	let year = now.getFullYear();
	let month = now.getMonth() + 1;
	let day = now.getDate();
	month = month < 10 ? "0" + month : month;
	day = day < 10 ? "0" + day : day;
	return year + "-" + month + "-" + day;
}

//获取当前时间（XX:XX:XX）
export function getCurTime() {
	let now = new Date();
	let hours = now.getHours();
	let minutes = now.getMinutes();
	let seconds = now.getSeconds();
	hours = hours < 10 ? "0" + hours : hours;
	minutes = minutes < 10 ? "0" + minutes : minutes;
	seconds = seconds < 10 ? "0" + seconds : seconds;

	return hours + ":" + minutes + ":" + seconds;
}

// utils/common.js
export function getCurDateTime(date, isEndDate = false) {
	if (!date) return null;
	
	// 深拷贝日期对象，避免污染原始数据
	const adjustedDate = new Date(date);
	
	if (isEndDate) {
	  adjustedDate.setHours(23, 59, 59, 999); // 设置为当天最后一毫秒
	} else {
	  adjustedDate.setHours(0, 0, 0, 0); // 开始时间设为 00:00:00
	}
  
	const year = adjustedDate.getFullYear();
	const month = String(adjustedDate.getMonth() + 1).padStart(2, '0');
	const day = String(adjustedDate.getDate()).padStart(2, '0');
	const hours = String(adjustedDate.getHours()).padStart(2, '0');
	const minutes = String(adjustedDate.getMinutes()).padStart(2, '0');
	const seconds = String(adjustedDate.getSeconds()).padStart(2, '0');
	return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  }


//向sessionStorage中存储一个JSON对象
export function setSessionStorage(keyStr, value) {
	sessionStorage.setItem(keyStr, JSON.stringify(value));
}

//从sessionStorage中获取一个JSON对象（取不到时返回null）
export function getSessionStorage(keyStr) {
	var str = sessionStorage.getItem(keyStr);
	if (str == '' || str == null || str == 'null' || str == undefined) {
		return null;
	} else {
		return JSON.parse(str);
	}
}

//从sessionStorage中获取一个非JSON对象（取不到时返回null）
export function getSessionStorageObj(keyStr) {
	var str = sessionStorage.getItem(keyStr);
	if (str == '' || str == null || str == 'null' || str == undefined) {
		return null;
	} else {
		//如果是非JSON对象，则做下转换
		const obj = JSON.parse(str);
		console.log(obj);
		return obj;
	}
}

//从sessionStorage中移除一个JSON对象
export function removeSessionStorage(keyStr) {
	sessionStorage.removeItem(keyStr);
}

//向localStorage中存储一个JSON对象
export function setLocalStorage(keyStr, value) {
	localStorage.setItem(keyStr, JSON.stringify(value));
}

//从localStorage中获取一个JSON对象（取不到时返回null）
export function getLocalStorage(keyStr) {
	var str = localStorage.getItem(keyStr);
	if (str == '' || str == null || str == 'null' || str == undefined) {
		return null;
	} else {
		return JSON.parse(str);
	}
}

//从localStorage中移除一个JSON对象
export function removeLocalStorage(keyStr) {
	localStorage.removeItem(keyStr);
}
